/**
 * 
 */
package com.surelution.cppei.card

import org.apache.commons.io.IOUtils;
import org.apache.http.Consts
import org.apache.http.HttpEntity
import org.apache.http.HttpResponse
import org.apache.http.NameValuePair
import org.apache.http.client.HttpClient
import org.apache.http.client.entity.UrlEncodedFormEntity
import org.apache.http.client.methods.HttpPost
import org.apache.http.impl.client.DefaultHttpClient
import org.apache.http.message.BasicNameValuePair
import org.apache.http.util.EntityUtils



/**
 * @author <a href="mailto:guangzong.syu@gmail.com">guangzong</a>
 * 这个类树仅用于查询卡信息
 */
abstract class CardInfo {

	abstract CardInfo query(String cardNo)
	
	public final String BASE_API_URL = "http://11.0.27.139:8080/petrosales-wechat-appserver-1.0/";
	
	private String cardNo
	
	public void setCardNo(String cardNo) {
		this.cardNo = cardNo
	}
	
	public String getCardNo() {
		cardNo
	}
	
	static String SUCCEED = '''
<DATA>
	<RESULT>1</RESULT>
	<CONSSET>
		<singleItem name="gender">1</singleItem>
		<singleItem name="name">潘泉锦</singleItem>
		<singleItem name="idnoType">1</singleItem>
		<singleItem name="idno">361121199011212414</singleItem>
		<singleItem name="ownerId">19425014</singleItem>
		<singleItem name="cardAsn">9010000000003601</singleItem>
		<singleItem name="userType">1</singleItem>	    
	</CONSSET>			
</DATA>
'''
	
	static String FAILED =  '''
<DATA>
	<RESULT>0</RESULT>
	<ERROR>没有找到此用户</ERROR>			
</DATA>
'''
	
	public static void main(String[] args) {
		
		String url = "http://11.0.27.139:8080/petrosales-wechat-appserver-1.0/getUserRegisterInfo.action";
		HttpPost post = new HttpPost(url);
		HttpClient client = new DefaultHttpClient();
		
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("key", "7/ZM+0PP9xTxMvQKPjpgYg=="));
		nvps.add(new BasicNameValuePair("cardAsn", "9110000000000117"));
		nvps.add(new BasicNameValuePair("idno", "130406198902060690"));
		post.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
		HttpResponse response = client.execute(post);
		HttpEntity entity = response.getEntity();
		InputStream is = entity.getContent();
//		HttpResponse response = client.execute(post);
		
		
//		IOUtils.readLines(is).each {
//			println it
//		}
		
		def records = new XmlParser().parse(is).CONSSET.singleItem
//		def records = new XmlParser().parseText(SUCCEED).CONSSET.singleItem
//		println records
		def name = records.find() {item->
			item.'@name'.contains('name')
		}
		println name.text()
		
//		def result = records.RESULT.text()
//		println result == "1"
		
		
		EntityUtils.consume(entity);
	}
}
